package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog findByOne(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> findAll() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Object query(StrategyCatalogQuery qo) {
        Query query = new Query();
        long count = mongoTemplate.count(query, StrategyCatalog.class);
        if (count==0) {
            return Page.empty();
        }
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        count = mongoTemplate.count(query, StrategyCatalog.class);
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageRequest);
        List<StrategyCatalog> list = mongoTemplate.find(query, StrategyCatalog.class);
        return new PageImpl<>(list,pageRequest,count);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {

    }

    //根据目的地id查询匹配的攻略分类
    @Override
    public List<StrategyCatalog> queryCatalogsByDestId(String destId) {
        //
        List<StrategyCatalog> strategyCatalogs = strategyCatalogRepository.findByDestId(destId);
        for (StrategyCatalog strategyCatalog : strategyCatalogs) {
            List<Strategy> strategys = strategyService.findByCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(strategys);
        }
        return strategyCatalogs;
    }

    //查询所有分类名字并查询
    @Override
    public List<CatalogVO> queryCatalogVOById() {
        //创建集合
        List<CatalogVO> list = new ArrayList<>();
        //查询所有分类对象
        List<StrategyCatalog> strategyCatalogs = this.findAll();
        //创建set集合存储destName字段
        Set<String> destNames=new HashSet<>();
        //得到唯一的destName(城市名称)
        for (StrategyCatalog strategyCatalog : strategyCatalogs) {
            destNames.add(strategyCatalog.getDestName());
        }
        //遍历集合
        for (String destName : destNames) {
            //用于封装数据的对象
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(destName);
            //destName作为条件查询攻略分类表
            catalogVO.setCatalogList(strategyCatalogRepository.findByDestName(destName));
            list.add(catalogVO);
        }
        return list;
    }

}
